﻿using System;
using Google.GData.Apps;
using Microsoft.ManagementConsole;

namespace Google.Apps.MMC.Users
    {
    public class UserNode : ResultNode
        {

        public UserNode(UserEntry entry)
            {
                this.Update(entry);
            }

        public void Update()
            {
            UserEntry newuser = GoogleAppsSnapIn.appservice.RetrieveUser(this.userentry.Login.UserName);
            this.Update(newuser);
            }

        private void Update(UserEntry entry)
            {
            this.userentry = entry;
            this.SubItemDisplayNames.Clear();
            this.DisplayName = this.userentry.Login.UserName;
            this.SubItemDisplayNames.Add(this.userentry.Name.GivenName);
            this.SubItemDisplayNames.Add(this.userentry.Name.FamilyName);
            if (this.userentry.Login.Admin)
                {
                this.SubItemDisplayNames.Add("Administrator");
                this.ImageIndex = GoogleAppsSnapIn.icon_user_admin;
                }
            else
                {
                this.SubItemDisplayNames.Add("User");
                this.ImageIndex = GoogleAppsSnapIn.icon_user_normal;
                }
            if (this.userentry.Login.Suspended)
                {
                this.SubItemDisplayNames.Add("Suspended");
                this.ImageIndex = GoogleAppsSnapIn.icon_user_suspended;
                }
            else if (!this.userentry.Login.AgreedToTerms)
                {
                this.SubItemDisplayNames.Add("Not Agreed To Terms");
                this.ImageIndex = GoogleAppsSnapIn.icon_user_noterms;
                }
            else
                {
                this.SubItemDisplayNames.Add("Normal");
                }
            string quota;
            if (this.userentry.Quota.Limit > 1024)
                {
                quota = Math.Round((float) (this.userentry.Quota.Limit / 1024), 1).ToString() + " GB";
                }
            else
                {
                quota = this.userentry.Quota.Limit.ToString() + " MB";
                }


            this.SubItemDisplayNames.Add(quota);
            }



        internal UserEntry userentry;

        }
    }
